package com.lxd.api.filter.Impl;

import com.lxd.common.enums.ExceptionEnums;
import com.lxd.common.exception.ApiException;
import com.lxd.api.feign.cacheClient;
import com.lxd.api.filter.CheckFilter;
import com.lxd.common.constants.CacheConstant;
import com.lxd.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author dongdong
 */
@Service(value = "apikey")
@Slf4j
public class ApiKeyCheckFilter implements CheckFilter {
    @Autowired
    private cacheClient cacheClient;

    @Override
    public void check(StandardSubmit submit) {
        log.info("【接口模块-校验ApiKey】   校验ing…………");
        //1.根据apikey从redis查询客户信息
        String key = CacheConstant.CLIENT_BUSINESS+submit.getApikey();
        log.info("【接口模块-校验ApiKey】  查询到key={}",key);
        Map value = cacheClient.hGetAll(key);
//        log.info("【接口模块-校验ApiKey】  查询到key={}的数据的value={}",key, value);
        //2.如果为空，抛出异常
        if (value==null||value.size()==0){
            log.info("【接口模块-校验ApiKey】  查询到key={}的数据不存在！！",key);
            throw new ApiException(ExceptionEnums.ERROR_APIKEY);
        }
        //3.不为空则封装数据
        log.info("【接口模块-校验ApiKey】key={}的数据的不为空",key);
            submit.setClientId(Long.parseLong(value.get("id")+""));


    }
}
